
const Koa = require('koa')
const app = new Koa()
const router = require('koa-router')()  /*引入实例化路由** 推荐*/


router.get('/',async (ctx)=>{
  ctx.body="首页"; 
})

router.get('/user', async (ctx, next) => {
  const { query } = ctx; // 获取get参数
  ctx.body = {
    user: 'hello user',
    query
  }
})

// 动态路由  参数  ctx.params.id获取
router.get('/user/:id', async (ctx, next) => {
  const { params } = ctx;
  ctx.body = {
    params
  }
})

router.post('/user', async (ctx, next) => {
  console.log(ctx.query)  // get
  ctx.type = 'application/json;charset=utf-8'
  console.log(ctx.request.body)  // post
  ctx.body = {
  }
})

app.use(router.routes(), router.allowedMethods());   /*启动路由*/

app.listen(9999, () => console.log(9999))